<!DOCTYPE html>
<html>
<head></head>
<body></body>
<script>

// Adds <link rel=prerender> for the URL.
function add_prerender(url) {
  const link = document.createElement('link');
  link.rel = 'prerender';
  link.href = url;
  document.head.appendChild(link);
}

async function unregister_service_worker(scope) {
  const registration = await navigator.serviceWorker.getRegistration(scope);
  if (!registration)
    return;
  return registration.unregister();
}

// Returns client urls exposed by Clients#matchAll().
async function get_exposed_client_urls() {
  // Start a service worker.
  const worker_url = 'clients_matchall_service_worker.js';
  const scope = 'controlled/';
  await unregister_service_worker(scope);
  const registration =
      await navigator.serviceWorker.register(worker_url, { scope: scope });

  // Ask the service worker to run Clients#matchAll().
  const message = await new Promise(resolve => {
    registration.installing.postMessage('matchall');
    navigator.serviceWorker.onmessage = resolve;
  });

  // `data` contains client URLs.
  return message.data;
}

</script>
</html>
